package com.roy.galaxy.thread;

/**
 * @author gang.wang1903@xiaobao100.cn
 * @Date: 2019-10-09 13:42
 *
 * 创建线程方式
 * Runable 与Thread两种方式
 * 有且仅有一个创建线程方式
 */
public class CreateThread {

    public static void main(String[] args) {

        myThread thread = new myThread();

        thread.start();


        Thread thread1 = new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"正在执行");
        }
        );
        thread1.start();
        //thread1.start();

        MyRunnable myRunnable = new MyRunnable();
        new Thread(myRunnable).start();
        new Thread(myRunnable).start();
        new Thread(myRunnable).start();
    }




    static class  myThread extends Thread{

         public void run() {
             System.out.println(Thread.currentThread().getName()+"正在执行");
         }
    }


    static class  MyRunnable implements Runnable {

        private Integer ticket = 10;

        @Override
        public void run() {
            fun();
            System.out.println(Thread.currentThread().getName() +this.hashCode()+ "正在执行");
        }

        public synchronized void fun() {
            for (int i = 0; i < 100; i++) {

                if (ticket > 0) {
                    System.out.println("卖票: ticket=" + ticket--);

                }

            }
        }
    }

}
